﻿using System;
using System.Threading;
using System.Windows.Forms;
using Relocation.Base;
using Relocation.Base.UI;
using Relocation.Com;
using Relocation.Data;
using Relocation.Service;

namespace Relocation
{
	public partial class LoginForm : BaseForm
	{
		/// <summary>
		/// 登录服务类
		/// </summary>
		private readonly UserService Service;		
		/// <summary>
		/// 登录用户
		/// </summary>
		public User User
		{
			get;
			private set;
		}

		public LoginForm()
			: base()
		{
			InitializeComponent();
			Initialize();
		}

		public LoginForm(Session session)
			: base(session)
		{
			InitializeComponent();
			Initialize();
			Service = new UserService(this.Session.DataModel);
		}

		/// <summary>
		/// 初始化配置以使用父类的功能
		/// </summary>
		private void Initialize()
		{
			//绑定验证信息
			this.username.Tag = new ControlTag("name", new ValidatorInfo(true), null);
			this.password.Tag = new ControlTag("password", new ValidatorInfo(true), null);			
		}
		

		/// <summary>
		/// 重写父类的事件（防止关闭窗体时提示数据未保存）
		/// </summary>		
		protected override void FieldTextChangeEvent(object sender, EventArgs e)
		{			
		}
		
		/// <summary>
		/// 登录
		/// </summary>		
		private void submit_Click(object sender, EventArgs e)
		{
			//验证用户输入
			if (!this.ValidatorAllControls())	
				return;
			try
			{
				String errorText;
				this.User = this.Service.Login(username.Text, Relocation.Com.Encode.MD5.HashString(password.Text), out errorText);
				if (this.User == null)
				{
					this.errLabel.Text = errorText;
					return;
				}
				this.FieldIsChanged = false;
				this.DialogResult = DialogResult.OK;				
				this.Close();
				
			} catch (System.Data.EntityException exEntity)
			{
				Log.Error(exEntity.GetInnerExceptionMessage());
				MyMessagebox.Error("数据库连接失败！");
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("登录失败,请联系管理员！");				
			}
		}

		/// <summary>
		/// 重置
		/// </summary>		
		private void reset_Click(object sender, EventArgs e)
		{
			this.ResetFields();
			errLabel.Text = "";
		}
		
		/// <summary>
		/// 用户名改变时清空密码和错误提示
		/// </summary>		
		private void username_TextChanged(object sender, EventArgs e)
		{
			this.password.Text = "";
			this.errLabel.Text = "";
		}

		/// <summary>
		/// 加载用户名列表
		/// </summary>		
		private void login_Load(object sender, EventArgs e)
		{
			ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object obj) {
				try
				{
					string[] userNameArr = this.Service.GetUserNameArray();
					MethodInvoker invoker = new MethodInvoker(delegate() {
						this.username.Items.Clear();
						this.username.Items.AddRange(userNameArr);

						//this.username.Text = "admin";
						//this.password.Text = "admin";
						//this.submit.PerformClick();
					});
					this.ExecMethodInvoker(invoker);					
				} catch (Exception ex)
				{
					Log.Error(ex.GetInnerExceptionMessage());
					Log.Error("加载用户连接失败");
					MyMessagebox.Error("加载用户名列表失败，请检查数据库连接");
				}				
			}));
		}
	}
}
